export const chaoshengbo2 = function (_, generator) {
    var Trig = this.getFieldValue('Trig');
    var Echo = this.getFieldValue('Echo');
    generator.setups_['setup_output_' + Trig] = 'pinMode(' + Trig + ', OUTPUT);';
    generator.setups_['setup_output_' + Echo] = 'pinMode(' + Echo + ', INPUT);';
    var funcName = 'checkdistance_' + Trig + '_' + Echo;
    var code = 'float' + ' ' + funcName + '() {\n'
        + '  digitalWrite(' + Trig + ', LOW);\n' + '  delayMicroseconds(2);\n'
        + '  digitalWrite(' + Trig + ', HIGH);\n' + '  delayMicroseconds(10);\n'
        + '  digitalWrite(' + Trig + ', LOW);\n'
        + '  float distance = pulseIn(' + Echo + ', HIGH) / 58.00;\n'
        + '  delay(10);\n' + '  return distance;\n'
        + '}\n';
    generator.definitions_[funcName] = code;
    return [funcName + '()', generator.ORDER_ATOMIC];
}

export const ds18b20 = function (_, generator) {
    var dropdown_pin = this.getFieldValue('PIN');
    var unit = this.getFieldValue('UNIT');
    generator.definitions_['include_OneWire'] = '#include <OneWireSTM.h>';
    generator.definitions_['include_DallasTemperature'] = '#include <DallasTemperature.h>';
    generator.definitions_['var_declare_OneWire_DallasTemperature_sensors_' + dropdown_pin] = 'OneWire oneWire_' + dropdown_pin + '(' + dropdown_pin + ');\nDallasTemperature sensors_' + dropdown_pin + '(&oneWire_' + dropdown_pin + ');';
    generator.definitions_['var_declare_DeviceAddress_insideThermometer'] = 'DeviceAddress insideThermometer;';
    generator.setups_['setup_sensors_' + dropdown_pin + '_getAddress'] = 'sensors_' + dropdown_pin + '.getAddress(insideThermometer, 0);';
    generator.setups_['setup_sensors_' + dropdown_pin + '_setResolution'] = 'sensors_' + dropdown_pin + '.setResolution(insideThermometer, 9);';
    var funcName = 'ds18b20_' + dropdown_pin + '_getTemp';
    var code = 'float' + ' ' + funcName + '(int w) {\n'
        + '  sensors_' + dropdown_pin + '.requestTemperatures();\n'
        + '  if(w==0) {\n    return sensors_' + dropdown_pin + '.getTempC(insideThermometer);\n  }\n'
        + '  else {\n    return sensors_' + dropdown_pin + '.getTempF(insideThermometer);\n  }\n'
        + '}\n';
    generator.definitions_[funcName] = code;
    return ['ds18b20_' + dropdown_pin + '_getTemp(' + unit + ')', generator.ORDER_ATOMIC];
}

// BMX280读取
export const bmx280_read_data = function (_, generator) {
    var TYPE = this.getFieldValue('TYPE');
    var dropdown_i2c_type = this.getFieldValue('I2C_TYPE');
    var dropdown_i2c_name = (dropdown_i2c_type == 'remap_I2C_1' ? 'Wire1' : dropdown_i2c_type);
    var address = generator.valueToCode(this, 'address', generator.ORDER_ATOMIC);
    generator.definitions_['include_Wire'] = '#include <Wire.h>';
    generator.definitions_['include_SPI'] = '#include <SPI.h>';
    generator.definitions_['include_Adafruit_Sensor'] = '#include <Adafruit_Sensor.h>';
    generator.definitions_['var_declare_' + dropdown_i2c_name] = 'TwoWire ' + dropdown_i2c_name + '(' + dropdown_i2c_type.charAt(dropdown_i2c_type.length - 1) + ', I2C_FAST_MODE);';
    if (TYPE == "bme") {
        generator.definitions_['include_Adafruit_BME280'] = '#include <Adafruit_BME280.h>';
        generator.definitions_['var_declare_Adafruit_BME280_' + dropdown_i2c_name] = 'Adafruit_BME280 bme_' + dropdown_i2c_name + ';';
    }
    else {
        generator.definitions_['include_Adafruit_BMP280'] = '#include <Adafruit_BMP280.h>';
        generator.definitions_['var_declare_Adafruit_BMP280_' + dropdown_i2c_name] = 'Adafruit_BMP280 bmp_' + dropdown_i2c_name + '(&' + dropdown_i2c_name + ');';
    }
    if (dropdown_i2c_type == 'remap_I2C_1')
        generator.setups_['setup_i2c_remap_I2C_1'] = 'afio_remap(AFIO_REMAP_I2C1);\n'
            + '  gpio_set_mode(GPIOB, 9, GPIO_AF_OUTPUT_OD);\n'
            + '  gpio_set_mode(GPIOB, 8, GPIO_AF_OUTPUT_OD);';
    if (TYPE == "bme") {
        generator.setups_['setup_Adafruit_BME280_' + dropdown_i2c_name] = TYPE + '_' + dropdown_i2c_name + '.begin(' + address + ', &' + dropdown_i2c_name + ');';
    } else {
        generator.setups_['setup_Adafruit_BMP280_' + dropdown_i2c_name] = TYPE + '_' + dropdown_i2c_name + '.begin(' + address + ');';
    }
    generator.definitions_['include_SEALEVELPRESSURE_HPA'] = '#define SEALEVELPRESSURE_HPA (1013.25)';
    var code = this.getFieldValue('BME_TYPE');
    return [TYPE + '_' + dropdown_i2c_name + '.' + code, generator.ORDER_ATOMIC];
}

// SHT20传感器
export const sht20_get_data = function (_, generator) {
    var dropdown_i2c_type = this.getFieldValue('I2C_TYPE');
    var dropdown_i2c_name = (dropdown_i2c_type == 'remap_I2C_1' ? 'Wire1' : dropdown_i2c_type);
    generator.definitions_['include_Wire'] = '#include <Wire.h>';
    generator.definitions_['include_DFRobot_SHT20'] = '#include <DFRobot_SHT20.h>';
    generator.definitions_['var_declare_' + dropdown_i2c_name] = 'TwoWire ' + dropdown_i2c_name + '(' + dropdown_i2c_type.charAt(dropdown_i2c_type.length - 1) + ', I2C_FAST_MODE);';
    generator.definitions_['var_declare_DFRobot_SHT20_' + dropdown_i2c_name] = 'DFRobot_SHT20 sht20_' + dropdown_i2c_name + ';\n';
    if (dropdown_i2c_type == 'remap_I2C_1')
        generator.setups_['setup_i2c_remap_I2C_1'] = 'afio_remap(AFIO_REMAP_I2C1);\n'
            + '  gpio_set_mode(GPIOB, 9, GPIO_AF_OUTPUT_OD);\n'
            + '  gpio_set_mode(GPIOB, 8, GPIO_AF_OUTPUT_OD);';
    generator.setups_['setup_sht20initSHT20_' + dropdown_i2c_name] = 'sht20_' + dropdown_i2c_name + '.initSHT20(' + dropdown_i2c_name + ');';
    generator.setups_['setup_sht20.checkSHT20_' + dropdown_i2c_name] = 'sht20_' + dropdown_i2c_name + '.checkSHT20();\n';
    var dropdown_type = this.getFieldValue('SHT20_TYPE');
    var code = 'sht20_' + dropdown_i2c_name + '.' + dropdown_type;
    return [code, generator.ORDER_ATOMIC];
}

import TEMPLATE_FUNCTION_TCS230_GET_COLOR from '../templates/cpp/function-tcs230-get-color.cpp';

// 初始化TCS230颜色传感器
export const tcs230_init = function (_, generator) {
    var value_tcs230_s0 = generator.valueToCode(this, 'tcs230_s0', generator.ORDER_ATOMIC);
    var value_tcs230_s1 = generator.valueToCode(this, 'tcs230_s1', generator.ORDER_ATOMIC);
    var value_tcs230_s2 = generator.valueToCode(this, 'tcs230_s2', generator.ORDER_ATOMIC);
    var value_tcs230_s3 = generator.valueToCode(this, 'tcs230_s3', generator.ORDER_ATOMIC);
    var value_tcs230_led = generator.valueToCode(this, 'tcs230_led', generator.ORDER_ATOMIC);
    var value_tcs230_out = generator.valueToCode(this, 'tcs230_out', generator.ORDER_ATOMIC);

    generator.definitions_['var_declare_tcs230_pin'] = '#define tcs230_S0 ' + value_tcs230_s0 + ''
        + '\n#define tcs230_S1 ' + value_tcs230_s1 + ''
        + '\n#define tcs230_S2 ' + value_tcs230_s2 + ''
        + '\n#define tcs230_S3 ' + value_tcs230_s3 + ''
        + '\n#define tcs230_sensorOut ' + value_tcs230_out + ''
        + '\n#define tcs230_LED ' + value_tcs230_led + '';

    generator.definitions_['function_tcs230_Getcolor'] = TEMPLATE_FUNCTION_TCS230_GET_COLOR;

    generator.setups_['setup_tcs230_pin'] = 'pinMode(tcs230_S0, OUTPUT);'
        + '\n  pinMode(tcs230_S1, OUTPUT);'
        + '\n  pinMode(tcs230_S2, OUTPUT);'
        + '\n  pinMode(tcs230_S3, OUTPUT);'
        + '\n  pinMode(tcs230_LED, OUTPUT);'
        + '\n  pinMode(tcs230_sensorOut, INPUT);'
        + '\n  digitalWrite(tcs230_S0,HIGH);'
        + '\n  digitalWrite(tcs230_S1,LOW);'
        + '\n  digitalWrite(tcs230_LED,HIGH);';
    var code = '';
    return code;
}

// TCS230颜色传感器 获取RGB值
export const tcs230_Get_RGB = function (_, generator) {
    var dropdown_tcs230_color = this.getFieldValue('tcs230_color');
    var code = 'tcs230_Getcolor(\'' + dropdown_tcs230_color + '\')';
    return [code, generator.ORDER_ATOMIC];
}

export const mpu6050_get_data = function (_, generator) {
    generator.definitions_['include_MPU6050_tockn'] = '#include <MPU6050_tockn.h>';
    generator.definitions_['include_Wire'] = '#include <Wire.h>';
    generator.definitions_['var_declare_mpu6050'] = 'MPU6050 mpu6050(Wire);';
    generator.setups_['setup_ngyro'] = 'Wire.setSDA(PB11);\n  Wire.setSCL(PB10);\n  Wire.begin();\n  mpu6050.begin();\n  mpu6050.calcGyroOffsets(true);';
    var MPU6050_TYPE = this.getFieldValue('MPU6050_TYPE');
    var code = 'mpu6050.' + MPU6050_TYPE;
    return [code, generator.ORDER_ATOMIC];
}

// 传感器-MPU6050-更新数据
export const mpu6050_update = function () {
    var code = 'mpu6050.update();\n';
    return code;
}
export const weightSensor = function (_, generator) {
    var DOUT = this.getFieldValue('DOUT');
    var SCK = this.getFieldValue('SCK');
    var scale = generator.valueToCode(this, 'scale', generator.ORDER_ATOMIC);
    generator.definitions_['include_Hx711'] = '#include <Hx711.h>';
    generator.definitions_['var_declare_Hx711' + DOUT + SCK] = 'Hx711 scale' + DOUT + '_' + SCK + "(" + DOUT + ", " + SCK + ");";
    generator.setups_['setup_HX711' + DOUT + SCK] = 'scale' + DOUT + '_' + SCK + '.setOffset(scale' + DOUT + '_' + SCK + '.getAverageValue(30));';
    generator.setups_['setup_' + 'scale' + DOUT + '_' + SCK + ' .setScale'] = 'scale' + DOUT + '_' + SCK + '.setScale(' + scale + ');';
    var code = 'scale' + DOUT + '_' + SCK + '.getWeight(10)';
    return [code, generator.ORDER_ATOMIC];
}

// 旋转编码器初始化
export const sensor_encoder_init = function (_, generator) {
    var dropdownType = this.getFieldValue('TYPE');
    var valueClk = generator.valueToCode(this, 'CLK', generator.ORDER_ATOMIC);
    var valueDt = generator.valueToCode(this, 'DT', generator.ORDER_ATOMIC);
    generator.definitions_['include_ESPRotary'] = '#include <ESPRotary.h>';
    generator.definitions_['var_declare_encoder' + dropdownType] = `ESPRotary encoder${dropdownType};`;
    generator.setups_['setup_encoder' + dropdownType] = `encoder${dropdownType}.begin(${valueDt}, ${valueClk});\n  encoder${dropdownType}.setStepsPerClick(2);`;
    var code = `encoder${dropdownType}.loop();\n`;
    return code;
}

// 旋转编码器读取
export const sensor_encoder_get = function (_, generator) {
    var dropdownType = this.getFieldValue('TYPE');
    var dropdownOperateType = this.getFieldValue('OPERATE_TYPE');
    var code = `encoder${dropdownType}.${dropdownOperateType}()`;
    return [code, generator.ORDER_ATOMIC];
}

// 旋转编码器设置
export const sensor_encoder_set = function (_, generator) {
    var dropdownType = this.getFieldValue('TYPE');
    var valueData = generator.valueToCode(this, 'DATA', generator.ORDER_ATOMIC);
    var dropdownOperateType = this.getFieldValue('OPERATE_TYPE');
    var code = `encoder${dropdownType}.${dropdownOperateType}(${valueData});\n`;
    return code;
}

// 旋转编码器事件
export const sensor_encoder_handle = function (_, generator) {
    var dropdownType = this.getFieldValue('TYPE');
    var dropdownOperateType = this.getFieldValue('OPERATE_TYPE');
    var statementsDo = generator.statementToCode(this, 'DO');
    var cbFuncName = 'encoder' + dropdownType;
    switch (dropdownOperateType) {
    case 'setChangedHandler':
        cbFuncName += 'OnChanged';
        break;
    case 'setRightRotationHandler':
        cbFuncName += 'OnRightRotation';
        break;
    case 'setLeftRotationHandler':
        cbFuncName += 'OnLeftRotation';
        break;
    case 'setUpperOverflowHandler':
        cbFuncName += 'OnUpperOverflow';
        break;
    case 'setLowerOverflowHandler':
    default:
        cbFuncName += 'OnLowerOverflow';
    }
    generator.definitions_['function_' + cbFuncName] = `void ${cbFuncName}(ESPRotary& encoder${dropdownType}) {\n`
        + `  ${statementsDo}`
        + `}\n`;
    generator.setups_['setup_' + cbFuncName] = `encoder${dropdownType}.${dropdownOperateType}(${cbFuncName});`;
    var code = '';
    return code;
}


